#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <pthread.h>

#define PORT 8080

int blink_interval = 0;
int stop_blinking = 0;
pthread_t blink_thread;

void set_led(int state) {
    if (state) {
        printf("LED is ON\n");
    } else {
        printf("LED is OFF\n");
    }
}

void initial_blink(int interval, int times) {
    for (int i = 0; i < times; i++) {
        set_led(1);
        sleep(interval);
        set_led(0);
        sleep(interval);
    }
}

void* blink_led(void* arg) {
    while (!stop_blinking) {
        set_led(1);
        sleep(blink_interval);
        set_led(0);
        sleep(blink_interval);
    }
    return NULL;
}

void handle_client(int client_socket) {
    char buffer[1024] = {0};
    while (1) {
        int valread = read(client_socket, buffer, 1024);
        if (valread > 0) {
            buffer[valread] = '\0';
            if (strcmp(buffer, "on") == 0) {
                stop_blinking = 1;
                pthread_join(blink_thread, NULL);
                set_led(1);
            } else if (strcmp(buffer, "off") == 0) {
                stop_blinking = 1;
                pthread_join(blink_thread, NULL);
                set_led(0);
            } else if (strncmp(buffer, "blink", 5) == 0) {
                int interval = atoi(buffer + 6);
                if (interval > 0) {
                    stop_blinking = 1;
                    pthread_join(blink_thread, NULL);
                    blink_interval = interval;
                    stop_blinking = 0;
                    pthread_create(&blink_thread, NULL, blink_led, NULL);
                } else {
                    printf("Invalid interval received: %s\n", buffer);
                }
            } else {
                printf("Invalid command received: %s\n", buffer);
            }
        } else {
            break;
        }
    }
    close(client_socket);
}

int main() {
    int server_fd, client_socket;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
        // 开机时 LED 闪烁
    initial_blink(1, 5);  // 每秒闪烁一次，闪烁5次

    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("Socket failed");
        exit(EXIT_FAILURE);
    }

    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
        perror("Setsockopt failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);

    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("Bind failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }

    if (listen(server_fd, 3) < 0) {
        perror("Listen failed");
        close(server_fd);
        exit(EXIT_FAILURE);
    }

    while (1) {
        if ((client_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
            perror("Accept failed");
            close(server_fd);
            exit(EXIT_FAILURE);
        }
        handle_client(client_socket);
    }

    return 0;
}